package com.github.slprj.slake.net;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * nio处理请求状态数据,主要提供了
 * - 处理器
 * - 业务数据处理线程池
 * - 限流器等
 *
 * @author YangXiaoLong
 * 2022/8/24 0:09
 */
public class NioStatus {
    /**
     * 处理器
     */
    private final ConcurrentHashMap<Integer, ReqProcessor> processors = new ConcurrentHashMap<>();
    /**
     * 记录请求字节数
     */
    private final AtomicInteger inReqBytes = new AtomicInteger();
    /**
     * 数据处理线程 , 默认为 核心数 * 4
     */
    private ExecutorService bizExecutor;
    /**
     * 限流器
     */
    private Semaphore requestSemaphore;

    /**
     * 注册处理器
     *
     * @param cmd       请求指令
     * @param processor 处理器
     * @author YangXiaoLong
     * 2022/10/12 17:55
     */
    public void registerProcessor(int cmd, ReqProcessor processor) {
        this.processors.put(cmd, processor);
    }

    public ConcurrentHashMap<Integer, ReqProcessor> getProcessors() {
        return processors;
    }

    public AtomicInteger getInReqBytes() {
        return inReqBytes;
    }

    public ExecutorService getBizExecutor() {
        return bizExecutor;
    }

    public void setBizExecutor(ExecutorService bizExecutor) {
        this.bizExecutor = bizExecutor;
    }

    public Semaphore getRequestSemaphore() {
        return requestSemaphore;
    }

    public void setRequestSemaphore(Semaphore requestSemaphore) {
        this.requestSemaphore = requestSemaphore;
    }
}
